//
//  Untitled.swift
//  demo01
//
//  Created by babybus on 2025/7/1.
//
import Foundation
import UIKit
import SnapKit


class CollectionSingle:UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    private var numsOfRow = 3
    
    private var items:[ColorBlock] = [
        ColorBlock(nameOfColor: "红色", typeOfColor: .red, showBorder: false),
        ColorBlock(nameOfColor: "黄色", typeOfColor: .yellow, showBorder: false),
        ColorBlock(nameOfColor: "蓝色", typeOfColor: .blue, showBorder: false),
        ColorBlock(nameOfColor: "绿色", typeOfColor: .green, showBorder: false),
        ColorBlock(nameOfColor: "紫色", typeOfColor: .purple, showBorder: false),
        ColorBlock(nameOfColor: "粉色", typeOfColor: .systemPink, showBorder: false),
    ]
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        attributeInit()
    }
    convenience init(frame: CGRect) {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        self.init(frame: frame, collectionViewLayout: layout)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func attributeInit(){
        backgroundColor = .white
        dataSource = self
        delegate = self
        register(ColorCell.self, forCellWithReuseIdentifier: "ColorCell")
        register(MyHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "MyHeader")
    }
    func setupUI(){
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ColorCell", for: indexPath)as! ColorCell
        let item = items[indexPath.row]
        cell.configure(colorname: item.nameOfColor, colortype: item.typeOfColor, showBorder: item.showBorder)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "MyHeaderView", for: indexPath)as! MyHeaderView
            return header
        }
        return UICollectionReusableView()
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (collectionView.bounds.width - CGFloat(numsOfRow)*10.0)/CGFloat(numsOfRow);
        return CGSize(width: width, height: 100)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 0)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)as! ColorCell
        let color_name = cell.getColorName()
        print("collectionView -- 颜色 -- " + color_name)
        cell.setColorButtonBorder()
    }
    
    func getNumsOfRow()->Int{return self.numsOfRow}
    func setNumsOfRow(number:Int){self.numsOfRow = number}
}
